package actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import bean.CreateUserFormBean;
import bean.User;
import dao.UserDAO;


public class CreateUserBestaetigungAction extends Action{


	@Override
	public String execute(HttpServletRequest request, HttpSession s) throws Exception {
		
		if(s.getAttribute("createUserFormBean") != null){
			CreateUserFormBean createUserFormBean = (CreateUserFormBean)s.getAttribute("createUserFormBean");
			String nachname = createUserFormBean.getNachname();
			String vorname = createUserFormBean.getVorname();
			String username = createUserFormBean.getUsername();
			String password = createUserFormBean.getPassword();
			String role = createUserFormBean.getRole();
			boolean isAdmin;

			isAdmin = !role.equals("Aktienhaendler");
			
			if(username.length() > 8){
				setApplicationMessage(s, getMessageProperty("benutzernameZuLang"));
				return getForwardProperty("createUser");
			}
			if(password.length() > 8){
				setApplicationMessage(s, getMessageProperty("passwortZuLang"));
				return getForwardProperty("createUser");
			}
			if(nachname.length() > 15){
				setApplicationMessage(s, getMessageProperty("nachnameZuLang"));
				return getForwardProperty("createUser");
			}
			if(vorname.length() > 15){
				setApplicationMessage(s, getMessageProperty("vornameZuLang"));
				return getForwardProperty("createUser");
			}
			
			User newUser = new User(nachname, vorname, username, password, isAdmin, 10000);
			if(UserDAO.getInstance().insertNewUser(newUser)){
				s.setAttribute("createUserFormBean", null);
				setApplicationMessage(s, getMessageProperty("benutzerErstellt") + username);
				return getForwardProperty("createUser");
			}
			else{
				setApplicationMessage(s, getMessageProperty("userExistiertBereits"));
				return getForwardProperty("createUser");
			}
		}
		else{
			setApplicationMessage(s, getMessageProperty("unerwarteterFehler"));
			return getForwardProperty("createUser");
		}
	}
	
	
	

}

